/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.currency;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import finbudget.shared.Constants;
import finbudgettable.client.ui.grid.cell.input.InputEditableCell;

import java.math.BigDecimal;

public class CurrencyCell extends InputEditableCell<BigDecimal> {

    public interface Resources extends ClientBundle {

        interface Style extends CssResource {

            @ClassName("b-currency-cell_with_error")
            String bCurrencyCell_with_error();

            @ClassName("b-currency-cell__text")
            String bCurrencyCell__text();

            @ClassName("b-currency-cell__input")
            String bCurrencyCell__input();

            @ClassName("b-currency-cell")
            String bCurrencyCell();

            @ClassName("b-currency-filter-panel")
            String bCurrencyFilterPanel();
        }

        @Source("CurrencyCell.css")
        Style style();
    }

    public interface Formatter {
        String format(BigDecimal value);
    }

    private static class DefaultFormatter implements Formatter {
        private final NumberFormat format = NumberFormat.getFormat(Constants.formatCurrency);

        @Override
        public String format(BigDecimal value) {
            return format.format(value); //value.toPlainString();
        }
    }

    public static final Resources RESOURCES = GWT.create(Resources.class);

    static {
        RESOURCES.style().ensureInjected();
    }

    private final Formatter formatter;

    public CurrencyCell(Formatter formatter, boolean nullable) {
        super(nullable);
        this.formatter = formatter;
    }

    public CurrencyCell(boolean nullable) {
        this(new DefaultFormatter(), nullable);
    }

    @Override
    protected String getValueToDisplay(BigDecimal value) {
        return value != null ? formatter.format(value) : " ";
    }

    @Override
    protected boolean validate(String value) {
        try {
            convertEditedValueToValue(value);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    @Override
    protected String convertValueToEditedValue(BigDecimal value) {
        return value != null ? value.toString() : "";
    }

    @Override
    protected BigDecimal convertEditedValueToValue(String editedValue) {
        if (isNullable() && (editedValue == null || editedValue.isEmpty())) {
            return null;
        } else {
            return new BigDecimal(editedValue.replaceAll(",", "."));
        }
    }

    @Override
    protected String getInnerCellClassName() {
        return RESOURCES.style().bCurrencyCell__text();
    }
}
